package com.example.ayniu.treadmill.util;

import android.os.Environment;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by Ayniu on 2017/4/11.
 */
public class UserInfoUtil {
    public static int thisSpeed = 0; // 0.1Km/s

    public static boolean setLocalPara(String key, String value) {
        boolean bRes = true;
        FileOutputStream fos = null;

        File appDir = new File(Environment.getExternalStorageDirectory(), "BeatWalk");
        if (!appDir.exists()) {
            bRes = appDir.mkdir();
            if(!bRes)
                return false;
        }

        appDir = new File(appDir.getAbsolutePath(), "source");
        if (!appDir.exists()) {
            bRes = appDir.mkdir();
            if(!bRes)
                return false;
        }

        appDir = new File(appDir.getAbsolutePath(), "para");
        if (!appDir.exists()) {
            bRes = appDir.mkdir();
            if(!bRes)
                return false;
        }

        String fileName = key + ".txt";
        File file = new File(appDir, fileName);
        try {
            if(!file.exists())
                file.createNewFile();
            fos = new FileOutputStream(file);
        }catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        if (fos == null)
            return false;

        try {
            fos.write(value.getBytes(), 0, value.length());
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }

        try {
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return bRes;
    };

    public static String getLocalPara(String key) {
        String inStr = "";
        byte [] inBuffer = new byte[1024];
        FileInputStream fos = null;

        File appDir = new File(Environment.getExternalStorageDirectory(), "BeatWalk");
        if (!appDir.exists()) {
            return inStr;
        }

        appDir = new File(appDir.getAbsolutePath(), "source");
        if (!appDir.exists()) {
            return inStr;
        }

        appDir = new File(appDir.getAbsolutePath(), "para");
        if (!appDir.exists()) {
            return inStr;
        }

        String fileName = key + ".txt";
        File file = new File(appDir, fileName);
        try {
            if(!file.exists())
                return inStr;
            fos = new FileInputStream(file);
        }catch (FileNotFoundException e) {
            e.printStackTrace();
            return inStr;
        }

        if (fos == null)
            return inStr;

        try {
            int len = fos.read(inBuffer, 0, 1020);
            if(len > 0) {
                inBuffer[len] = '\0';
                inStr = new String(inBuffer); // 内存泄露？
                inStr = inStr.substring(0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return inStr;
        }

        try {
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return inStr;
        } catch (IOException e) {
            e.printStackTrace();
            return inStr;
        }
        return inStr;
    };
}
